import json
from django.shortcuts import HttpResponse
from service.company_node_service import CompanyNodeService

#查询节点
def query_company_node(request):
    if request.method == 'GET':
        company_node_service = CompanyNodeService()
        res=company_node_service.query_company_node()
        if len(res)>0:
            data={"code":200,"msg":"查询成功","data":res}
        else:
            data={"code":500,"msg":"没查询到数据","data":res}
        return HttpResponse(json.dumps(data))

#创建节点
def create_company_node(request):
    if request.method == 'GET':
        company_node_service = CompanyNodeService()
        #获取前端发送的请求
        reg_date=request.GET.get('reg_date')
        risk_level=request.GET.get('risk_level')
        reg_no=request.GET.get('reg_no')
        name=request.GET.get('name')
        registered_capital=request.GET.get('registered_capital')
        industry=request.GET.get('industry')
        legal_rep=request.GET.get('legal_rep')
        status=request.GET.get('status')
        res=company_node_service.create_company_node(
                reg_date=reg_date,
                risk_level=risk_level,
                reg_no=reg_no,
                name=name,
                registered_capital=registered_capital,
                industry=industry,
                legal_rep=legal_rep,
                status=status
            )
        if res == "创建成功":
                data={"code":200,"msg":"创建成功","data":res}
                return HttpResponse(json.dumps(data))
        else:
            data={"code":500,"msg":"创建失败","data":res}
            return HttpResponse(json.dumps(data))

#更新节点
def update_company_node(request):
    if request.method == 'GET':
        company_node_service = CompanyNodeService()
        # 获取前端发送的请求
        reg_date = request.GET.get('reg_date')
        risk_level = request.GET.get('risk_level')
        reg_no = request.GET.get('reg_no')
        name = request.GET.get('name')
        registered_capital = request.GET.get('registered_capital')
        industry = request.GET.get('industry')
        legal_rep = request.GET.get('legal_rep')
        status = request.GET.get('status')

        res=company_node_service.update_company_node(
                reg_date=reg_date,
                risk_level=risk_level,
                reg_no=reg_no,
                name=name,
                registered_capital=registered_capital,
                industry=industry,
                legal_rep=legal_rep,
                status=status
            )
        if res == "更新节点成功":
                data = {"code": 200, "msg": "更新成功", "data": res}
                return HttpResponse(json.dumps(data))
        else:
            data = {"code": 500, "msg": "更新失败", "data": res}
            return HttpResponse(json.dumps(data))

#删除节点
def delete_company_node(request):
    if request.method == 'GET':
        company_node_service = CompanyNodeService()
        reg_no=request.GET.get('reg_no')

        res = company_node_service.delete_company_node(reg_no=reg_no)
        if res == "删除节点成功":
            data = {"code": 200, "msg": "删除成功", "data": res}
            return HttpResponse(json.dumps(data))
        else:
            data = {"code": 500, "msg": "删除失败", "data": res}
            return HttpResponse(json.dumps(data))